package com.our.sys.project1.web;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.our.sys.common.utils.AppUtil;
import com.our.sys.project1.entity.DMaterials;
import com.our.sys.project1.entity.SMaterials;
import com.our.sys.project1.service.SMaterialsService;

@Controller
@RequestMapping("/smaterials")
public class SMaterialsController {

    private static Logger logger = LoggerFactory.getLogger(SMaterialsController.class);

    @Autowired
    private SMaterialsService sMaterialsService;

    @RequestMapping(value = "/query_s")
    public @ResponseBody
    Map<String, Object> queryS(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<SMaterials> page = new Page<SMaterials>(request);
        String orderColumn = request.getParameter("sort");
        String dir = request.getParameter("dir");
        if (StringUtils.isBlank(orderColumn)) {
            page.setOrderBy("createTime");
            page.setOrder(Page.DESC);
        } else {
            page.setOrderBy(orderColumn);
            page.setOrder(dir);
        }

        List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
        //UserSession u = (UserSession) request.getSession().getAttribute("userSession");
        String name = request.getParameter("name");
        if (StringUtils.isNotBlank(name)) {
            PropertyFilter filter = new PropertyFilter("LIKES_name_OR_py_OR_wb", name.trim());
            filters.add(filter);
        }

        Page<SMaterials> data = sMaterialsService.queryS(page, filters);
        map = AppUtil.buildJSONDataResponse(data.getResult(), (long) data.getTotalCount());
        return map;
    }

    @RequestMapping(value = "/query_d")
    public @ResponseBody
    Map<String, Object> queryD(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        Page<DMaterials> page = new Page<DMaterials>(request);
        String orderColumn = request.getParameter("sort");
        String dir = request.getParameter("dir");
        if (StringUtils.isBlank(orderColumn)) {
            page.setOrderBy("createTime");
            page.setOrder(Page.DESC);

        } else {
            page.setOrderBy(orderColumn);
            page.setOrder(dir);
        }

        List<PropertyFilter> filters = new ArrayList<PropertyFilter>();
        //UserSession u = (UserSession) request.getSession().getAttribute("userSession");
        String name = request.getParameter("name");
        if (StringUtils.isNotBlank(name)) {
            PropertyFilter filter = new PropertyFilter("LIKES_name_OR_py_OR_wb", name.trim());
            filters.add(filter);
        }

        String sId = request.getParameter("sId");
        if (StringUtils.isNotBlank(sId)) {
            PropertyFilter filter = new PropertyFilter("EQL_sId", sId);
            filters.add(filter);
        }
        Page<DMaterials> data = sMaterialsService.queryD(page, filters);
        map = AppUtil.buildJSONDataResponse(data.getResult(), (long) data.getTotalCount());
        return map;
    }

    @RequestMapping(value = "/save_s")
    public @ResponseBody
    Map<String, Object> saveS(SMaterials sMaterials) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            sMaterialsService.saveS(sMaterials);
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/save_d")
    public @ResponseBody
    Map<String, Object> saveD(DMaterials dMaterials) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            sMaterialsService.saveD(dMaterials);
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/delete_s")
    public @ResponseBody
    Map<String, Object> deleteS(@RequestParam("id") String id) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            sMaterialsService.deleteS(Long.valueOf(id));
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/delete_d")
    public @ResponseBody
    Map<String, Object> deleteD(@RequestParam("id") String id) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            sMaterialsService.deleteD(Long.valueOf(id));
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }

    @RequestMapping(value = "/disable_d")
    public @ResponseBody
    Map<String, Object> disableD(@RequestParam("id") String id) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            sMaterialsService.deleteD(Long.valueOf(id));
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }
    
    @RequestMapping(value = "/disable_s")
    public @ResponseBody
    Map<String, Object> disableS(@RequestParam("id") String id) throws Exception {
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            sMaterialsService.deleteD(Long.valueOf(id));
            map.put("success", true);
            map.put("message", "");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            map.put("success", false);
            map.put("message", e.getMessage());
        }
        return map;
    }
}
